#!/bin/bash
#jenkis configure options Operation <Deploy|Rollback>
#jenkins configure string ROOLLBACK_COMMITID <latest|6acffce>	
GITDIR="/var/lib/jenkins/workspace"
PRONAME="yyg"
DSTDIR="/opt/deploy"
#DATE="`date +%F_%H-%M`"
USER="www"
cat > /home/www/ip.list <<EOF
10.2.11.237 

10.2.11.233 

EOF
function deploy(){
       cd ${GITDIR}/${PRONAME} && git pull origin master
       COMMITID=$(git rev-parse --short HEAD)
       cd $GITDIR && tar zcf ${PRONAME}_${COMMITID}.tar.gz  ${PRONAME}/*
       mv ${PRONAME}_${COMMITID}.tar.gz /opt/deploy/
       for IP in `cat /home/www/ip.list`;do
                scp /opt/deploy/${PRONAME}_${COMMITID}.tar.gz ${USER}@${IP}:${DSTDIR}
                ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${COMMITID}.tar.gz && mv ${PRONAME} ${PRONAME}_${COMMITID}"
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${COMMITID} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
       done
}
function rollback(){
	cd ${GITDIR}/${PRONAME}
	if [ ${ROOLLBACK_COMMITID} == "latest" ];then
	    git reset --hard HEAD^
        COMMITID=$(git rev-parse --short HEAD)
        cd $GITDIR && tar zcf ${PRONAME}_${COMMITID}.tar.gz  ${PRONAME}/*
        mv ${PRONAME}_${COMMITID}.tar.gz /opt/deploy/
        for IP in `cat /home/www/ip.list`;do
        	scp /opt/deploy/${PRONAME}_${COMMITID}.tar.gz ${USER}@${IP}:${DSTDIR}
            ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${COMMITID}.tar.gz && mv ${PRONAME} ${PRONAME}_${COMMITID}"
        	ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln  -s /opt/deploy/${PRONAME}_${COMMITID} /var/www/html/${PRONAME}"
            ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
        done
	else
	    git reset --hard ${ROOLLBACK_COMMITID} 
        #online 
        for IP in `cat /home/www/ip.list`;do
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${ROOLLBACK_COMMITID} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
        done
    fi
}
case $Operation in 
Deploy)
  deploy
  ;;
Rollback)
  rollback
  ;;
*)
echo "Usage $0 <Deploy|Rollback>"
esac
GITDIR="/var/lib/jenkins/workspace"
PRONAME="yyg"
DSTDIR="/opt/deploy"
#DATE="`date +%F_%H-%M`"
USER="www"
cat > /home/www/ip.list <<EOF
10.2.11.237 

10.2.11.233 

EOF
function deploy(){
       cd ${GITDIR}/${PRONAME} && git pull origin master
       COMMITID=$(git rev-parse --short HEAD)
       cd $GITDIR && tar zcf ${PRONAME}_${COMMITID}.tar.gz  ${PRONAME}/*
       mv ${PRONAME}_${COMMITID}.tar.gz /opt/deploy/
       for IP in `cat /home/www/ip.list`;do
                scp /opt/deploy/${PRONAME}_${COMMITID}.tar.gz ${USER}@${IP}:${DSTDIR}
                ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${COMMITID}.tar.gz && mv ${PRONAME} ${PRONAME}_${COMMITID}"
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${COMMITID} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
       done
}
function rollback(){
	cd ${GITDIR}/${PRONAME}
	if [ ${ROOLLBACK_COMMITID} == "latest" ];then
	    git reset --hard HEAD^
        COMMITID=$(git rev-parse --short HEAD)
        cd $GITDIR && tar zcf ${PRONAME}_${COMMITID}.tar.gz  ${PRONAME}/*
        mv ${PRONAME}_${COMMITID}.tar.gz /opt/deploy/
        for IP in `cat /home/www/ip.list`;do
        	scp /opt/deploy/${PRONAME}_${COMMITID}.tar.gz ${USER}@${IP}:${DSTDIR}
            ssh ${USER}@${IP} "cd /opt/deploy && tar xf ${PRONAME}_${COMMITID}.tar.gz && mv ${PRONAME} ${PRONAME}_${COMMITID}"
        	ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln  -s /opt/deploy/${PRONAME}_${COMMITID} /var/www/html/${PRONAME}"
            ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
        done
	else
	    git reset --hard ${ROOLLBACK_COMMITID} 
        #online 
        for IP in `cat /home/www/ip.list`;do
                ssh ${USER}@${IP} "cd /var/www/html && rm -rf ${PRONAME} && ln -s /opt/deploy/${PRONAME}_${ROOLLBACK_COMMITID} /var/www/html/${PRONAME}"
                ssh ${USER}@${IP} "chown -R www.www 

 /var/www/html/${PRONAME}"
        done
    fi
}
case $Operation in 
Deploy)
  deploy
  ;;
Rollback)
  rollback
  ;;
*)
echo "Usage $0 <Deploy|Rollback>"
esac
